﻿'******************************* File Header *********************************
'* File Name:    MainForm.vb
'* Project:      WmiConnection.TestUI
'* Copyright (c) Freddy Kuehne
'*
'* MainForm of the test project for the wmiconnection project.
'* 
'* This source is subject to the GNU Library General Public License (LGPL).
'* See http://wmiconnection.codeplex.com/license.
'* All other rights reserved.
'*****************************************************************************

Imports System.Globalization

Public Class MainForm
#Region "Variables"
  Private m_Con As WmiConnection
#End Region

#Region "Properties"
  Private ReadOnly Property IsConnected As Boolean
    Get
      IsConnected = (m_Con IsNot Nothing) AndAlso (m_Con.State = ConnectionState.Open)
    End Get
  End Property
#End Region

#Region "Eventhandler"
  Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    cbQuery.SelectedIndex = 0
    Text += String.Format(CultureInfo.InvariantCulture, " ({0})", GetType(WmiConnection).Assembly.GetName.Version.ToString)
  End Sub

  Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
    Try
      m_Con = New WmiConnection(New WmiConnectionStringBuilder(txServer.Text, cbPath.Text, txUser.Text, txPassword.Text).ConnectionString)
      m_Con.Open()
      lbServerVersion2.Text = m_Con.ServerVersion
    Finally
      CheckControlState()
    End Try
  End Sub

  Private Sub cmdDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisconnect.Click
    If (m_Con IsNot Nothing) Then
      lbServerVersion2.Text = String.Empty
      m_Con.Close()
      m_Con = Nothing
      gridData.DataSource = Nothing
      CheckControlState()
    End If
  End Sub

  Private Sub cmdExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExecute.Click
    Dim dt As DataTable = Nothing
    Dim cmd As IDbCommand

    Try
      dt = New DataTable
      dt.Locale = CultureInfo.CurrentUICulture
      cmd = m_Con.CreateCommand
      cmd.CommandText = cbQuery.Text
      dt.Load(cmd.ExecuteReader)
      gridData.DataSource = dt.DefaultView
    Finally
      CheckControlState()
    End Try
  End Sub

  Private Sub cbQuery_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbQuery.TextChanged
    CheckControlState()
  End Sub
#End Region

#Region "Methods"
  Private Sub CheckControlState()
    Dim con As Boolean = IsConnected
    cmdConnect.Enabled = Not con
    cmdDisconnect.Enabled = con
    cmdExecute.Enabled = (cbQuery.Text.Length > 0)
    groupQuery.Enabled = con
  End Sub
#End Region
End Class
